package sing.core.util

import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import sing.core.base.BaseApp
import java.util.*

object AppUtil {
    private const val TAG = "AppUtil"

    /**
     * 获取当前进程的名称，默认进程名称是包名
     */
    val currentProcessName: String?
        get() {
            val pid = android.os.Process.myPid()
            val mActivityManager = BaseApp.getInstance().getSystemService(
                Context.ACTIVITY_SERVICE
            ) as ActivityManager
            for (appProcess in mActivityManager.runningAppProcesses) {
                if (appProcess.pid == pid) {
                    return appProcess.processName
                }
            }
            return null
        }

    /**
     * @return 获取所有的带启动图标的App
     */
    val launcherResolveInfoList: List<ResolveInfo>
        get() {
            val intent = Intent()
            intent.action = Intent.ACTION_MAIN
            intent.addCategory(Intent.CATEGORY_LAUNCHER)
            return BaseApp.getInstance().packageManager.queryIntentActivities(intent, 0)
        }

    /**
     * 取得版本名称
     */
    val versionName: String
        get() = defPackageInfo!!.versionName

    /**
     * 取得版本号
     */
    val versionCode: Int
        get() = defPackageInfo!!.versionCode

    /**
     * 获取应用的信息，0代表是获取版本信息
      */
    val defPackageInfo: PackageInfo?
        get() {
            val packageManager = BaseApp.getInstance().packageManager
            var packInfo: PackageInfo? = null
            try {
                packInfo = packageManager.getPackageInfo(BaseApp.getInstance().packageName, 0)
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }

            return packInfo
        }

    /**
     *
     * [获取应用程序版本名称信息]
     *
     * @param context
     *
     * @return 当前应用的版本名称
     */
    @Synchronized
    fun getPackageName(context: Context): String? {
        try {
            val packageManager = context.packageManager
            val packageInfo = packageManager.getPackageInfo(
                context.packageName, 0
            )
            return packageInfo.packageName
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * 系统分享文本的 Intent
     */
    fun shareTextIntent(text: String): Intent {
        val sendIntent = Intent()
        sendIntent.action = Intent.ACTION_SEND
        sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        sendIntent.putExtra(Intent.EXTRA_TEXT, text)
        sendIntent.type = "text/plain"
        return sendIntent
    }


    /**
     * 指定包名[packageName]的 App 是否已安装到设备上
     */
//    fun isAppExist(packageName: String): Boolean {
//        val packageManager = BaseApp.getInstance().packageManager
//        // 获取所有已安装程序的包信息
//        val list = packageManager.getInstalledPackages(0)
//        for (i in list.indices) {
//            if (list[i].packageName.equals(packageName, ignoreCase = true)) {
//                return true
//            }
//        }
//        return false
//    }

    /**
     * 打开应用的详细信息设置
     */
    fun gotoAppDetailsSettings(context: Context, requestCode: Int, packageName: String = context.packageName) {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.fromParts("package", packageName, null)
        if (context is Activity) {
            context.startActivityForResult(intent, requestCode)
        }
    }

    /**
     * 打开指定包名的应用
     */
    fun startActivityByPackage(context: Context, packageName: String): Boolean {
        val pm = context.packageManager
        val intent = pm.getLaunchIntentForPackage(packageName)
        if (null != intent) {
            context.startActivity(intent)
            return true
        }
        return false
    }

    /**
     * 打开其他的Activity
     *
     * @param cls 要打开的Activity
     */
    fun startActivity(context: Context?, cls: Class<*>) {
        if (context == null) {
            return
        }
        context.startActivity(Intent(context, cls))
    }

    fun startActivity(context: Context, cls: Class<*>, name: String, value: Int) {
        val intent = Intent(context, cls)
        intent.putExtra(name, value)
        context.startActivity(intent)
    }

    fun startActivity(context: Context, cls: Class<*>, name: String, value: Boolean) {
        val intent = Intent(context, cls)
        intent.putExtra(name, value)
        context.startActivity(intent)
    }

    /**
     * 打开其他的Activity，并附带字符串
     *
     * @param cls   要打开的Activity
     * @param name  字符串名称
     * @param value 字符串值
     */
    fun startActivity(context: Context, cls: Class<*>, name: String, value: String) {
        val intent = Intent(context, cls)
        intent.putExtra(name, value)
        context.startActivity(intent)
    }

    fun startActivityForResult(
        context: Activity, cls: Class<*>, requestCode: Int,
        name: String, value: String
    ) {
        val intent = Intent(context, cls)
        intent.putExtra(name, value)
        context.startActivityForResult(intent, requestCode)
    }

    /**
     * true，则说明引入了 retrofit2RxJava2
     */
    fun isRetrofitUseRx(): Boolean {
        var applicationInfo: ApplicationInfo? = null
        try {
            applicationInfo = BaseApp.getInstance().packageManager.getApplicationInfo(BaseApp.getInstance().packageName, PackageManager.GET_META_DATA)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        if (applicationInfo == null) return false
        return applicationInfo.metaData.getBoolean("isRetrofitUseRx")
    }

    /**
     * 确保已经获取到权限
     */
    fun getIMEI(context: Context): String {
        if (Build.VERSION.SDK_INT >= 28) {
            return ""
        }
        val manager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return try {
            val method = manager.javaClass.getMethod("getImei", Int::class.javaPrimitiveType)
            method.invoke(manager, 0) as String
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
            ""
        }
    }

    /**
     * 获取当前手机系统语言。
     * @return 返回当前系统语言。例如：当前设置的是“中文-中国”，则返回“zh-CN”
     */
    fun getSystemLanguage(): String = Locale.getDefault().language

    /**
     * 获取当前手机系统版本号
     * @return 系统版本号
     */
    fun getSystemVersion(): String = Build.VERSION.RELEASE

    /**
     * 获取手机型号
     * @return 手机型号
     */
    fun getSystemModel(): String = Build.MODEL

    /**
     * 获取手机厂商
     * @return 手机厂商
     */
    fun getDeviceBrand(): String = Build.BRAND
}